package com.ctrip.framework.apollo.portal.service;

import com.ctrip.framework.apollo.portal.enricher.AdditionalUserInfoEnricher;
import com.ctrip.framework.apollo.portal.enricher.adapter.UserInfoEnrichedAdapter;
import com.ctrip.framework.apollo.portal.entity.bo.UserInfo;
import com.ctrip.framework.apollo.portal.spi.UserService;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

@Service
public class AdditionalUserInfoEnrichServiceImpl implements AdditionalUserInfoEnrichService {

    private final UserService userService;
    private final List<AdditionalUserInfoEnricher> enricherList;

    public AdditionalUserInfoEnrichServiceImpl(UserService userService, List<AdditionalUserInfoEnricher> enricherList) {
        this.userService = userService;
        this.enricherList = enricherList;
    }

    @Override
    public <T> void enrichAdditionalUserInfo(List<? extends T> list, Function<? super T, ? extends UserInfoEnrichedAdapter> mapper) {
        if (CollectionUtils.isEmpty(list)) {
            return;
        }
        if (CollectionUtils.isEmpty(this.enricherList)) {
            return;
        }
        List<UserInfoEnrichedAdapter> adapterList = this.adapt(list, mapper);
        if (CollectionUtils.isEmpty(adapterList)) {
            return;
        }
        Set<String> userIdSet = this.extractOperatorId(adapterList);
        if (CollectionUtils.isEmpty(userIdSet)) {
            return;
        }
        List<UserInfo> userInfoList = this.userService.findByUserIds(new ArrayList<>(userIdSet));
        if (CollectionUtils.isEmpty(userInfoList)) {
            return;
        }
        Map<String, UserInfo> userInfoMap = userInfoList.stream().collect(Collectors.toMap(UserInfo::getUserId, Function.identity()));
        for (UserInfoEnrichedAdapter adapter : adapterList) {
            for (AdditionalUserInfoEnricher enricher : this.enricherList) {
                enricher.enrichAdditionalUserInfo(adapter, userInfoMap);
            }
        }
    }

    private <T> List<UserInfoEnrichedAdapter> adapt(List<? extends T> dtoList, Function<? super T, ? extends UserInfoEnrichedAdapter> mapper) {
        List<UserInfoEnrichedAdapter> adapterList = new ArrayList<>(dtoList.size());
        for (T dto : dtoList) {
            if (dto == null) {
                continue;
            }
            UserInfoEnrichedAdapter enrichedAdapter = mapper.apply(dto);
            adapterList.add(enrichedAdapter);
        }
        return adapterList;
    }

    private <T> Set<String> extractOperatorId(List<UserInfoEnrichedAdapter> adapterList) {
        Set<String> operatorIdSet = new HashSet<>();
        for (UserInfoEnrichedAdapter adapter : adapterList) {
            if (StringUtils.hasText(adapter.getFirstUserId())) {
                operatorIdSet.add(adapter.getFirstUserId());
            }
            if (StringUtils.hasText(adapter.getSecondUserId())) {
                operatorIdSet.add(adapter.getSecondUserId());
            }
            if (StringUtils.hasText(adapter.getThirdUserId())) {
                operatorIdSet.add(adapter.getThirdUserId());
            }
        }
        return operatorIdSet;
    }
}
